<?php

require_once 'UC_DBBase.php';
require_once 'UC_ColumnCollection.php';

class UC_Table extends UC_DBBase
{
    public function __construct()
    {
        
    }
    
    public function Associators_()
    {
        
    }
    
    public function Clone_()
    {
        
    }
    
    public function CompareTo_()
    {
        
    }
            
    public function Delete_()
    {
        
    }
            
    public function ExecuteMethod_()
    {
        
    }
    
    static function Exists_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Table())
        {
            $sql = "Select TABLE_NAME From INFORMATION_SCHEMA.TABLES " .
                "Where TABLE_SCHEMA = '" . $object_path->get_Schema() . "'" .
                "And TABLE_NAME = '" . $object_path->get_Name() . "'";
            
            try
            {
                if($object_path->get_Server()->get_PDO()->query($sql)->rowCount() == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (PDOException $e)
            {
                throw new UC_DBException(
                    'Unable to determine if the table exists.');
            }
        }
        else
        {
            return false;
        }
    }
    
    static function Get_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Table())
        {
            if(self::Exists_($object_path))
            {
                $table = new UC_Table();
                $table->set_Path_($object_path);
                return $table;
            }
            else
            {
                throw new InvalidArgumentException(
                    'Invalid object path.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid object path.');
        }
    }
    
    static function Instances_()
    {
        
    }
    
    public function Put_()
    {
        
    }
    
    public function References_()
    {
        
    }
    
    static function SpawnInstance_()
    {
        
    }
    
    public function get_Name()
    {
        return $this->get_Path_()->get_Name();
    }
    
    public function set_Name($Name)
    {
        if(is_string($Name))
        {
            $this->get_Path_()->set_Name($Name);
        }
        else
        {
            throw new InvalidArgumentException(
                'The name should be a string.');
        }
    }
    
    public function get_Columns()
    {
        if(!$this->Columns instanceof UC_ColumnCollection)
        {
            $this->Columns = new UC_ColumnCollection;
            $this->PopulateColumns();
        }
        
        return $this->Columns;
    }

    public function set_Columns(UC_ColumnCollection $Columns)
    {
        $this->Columns = $Columns;
    }

    private function PopulateColumns()
    {
        $sql = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS " .
            "Where TABLE_SCHEMA = '" . $this->get_Path_()->get_Schema() . "' " .
            "And TABLE_NAME = '" . $this->get_Path_()->get_Table() . "'";
        
        try
        {
            foreach($this->get_Path_()->get_Server()->get_PDO()->query($sql) as $c)
            {
                $path = new UC_DBObjectPath();
                $path->set_is_Column(true);
                $path->set_Server($this->get_Path_()->get_Server());
                $path->set_Schema($this->get_Path_()->get_Schema());
                $path->set_Table($this->get_Path_()->get_Table());
                $path->set_Name($c['COLUMN_NAME']);

                $column = UC_Column::Get_($path);
                $this->Columns->add($column);
            }
        }
        catch (PDOException $e)
        {
            throw new UC_DBException('Unable to enumerate columns.');
        }
    }
    
    private $Columns;
}

?>
